package com.yonyou.pmclouds.repeatwork.process;

import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.DateUtils;
import com.yonyou.pmclouds.project.entity.ProjectConst;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.repeatwork.entity.RepeatWorkVO;
import com.yonyou.pmclouds.workstopreport.entity.WorkStopVO;
import com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 校验当前项目相应的停工单业务关系：
 * 1、检查当前项目是否停工。
 * 2、检查复工单的复工时间要晚于对应停工单的停工时间。
 */
@Component
public class CheckWorkStop4ProjectProcessor implements SingleProcessor<RepeatWorkVO> {
    @Autowired
    private ProjectSimpleQuery projectSimpleQuery;
    @Autowired
    private WorkStopQuery workStopQuery;

    @Override
    public void process(RepeatWorkVO vo, RepeatWorkVO orig) throws BusinessException{
        String pk_project = vo.getPkProject();
        ProjectVO projectVO = projectSimpleQuery.queryProject(pk_project);

        if(projectVO.getProjectState() != ProjectConst.STATE_PAUSE) {
            throw new BusinessException("项目未停工，不需要进行复工");
        }
        int[] status = {BillStatusConst.COMMON_APPROVED_STATUS};
        WorkStopVO[] vos = workStopQuery.queryByStatus(pk_project,status);
        /**
         * 注：查询某项目审批通过的停工单是按照创建时间降序的，所以此处选择第一条停工单数据即未最新的停工单数据。
         */
        if(vos !=null && vos.length>0){
            long currworkStopDays = DateUtils.getDaysBetween(vos[0].getStopDate(),vo.getRepeatDate());
            if(currworkStopDays<0){
                throw new BusinessException("当前项目的复工日期不能晚于停工日期，请检查！");
            }
        }

    }
}
